/**
 * Created by zhu on 2015/11/1.
 */
var category;
var mydiv;
$(function () {
    mydiv = $('#my_select');

    $('#my_select').on('change', "select", function () {
        var length = mydiv.children('select').length - 1;
        var index = $(this).index();
        for (var i = length; i > index; i--) {
            mydiv.children('select')[i].remove();
        }
        //$('#my_select').children('select').attr("name","");
        var tempValue = $(this).children('option:selected').val();
        judge(tempValue);
    });

    $.get(contextPath + "/category", function (data) {
        loadData(data);
    });

});

function loadData(data) {
    data = jQuery.parseJSON(data);
    category = data.list;
    $("#select_first").empty();
    $.each(category, function (i, item) {
        if (item.parent_id == 0) {
            $("#select_first").append('<option value="' + item.id + '">' + item.name + '</option>');
        }
    });

}

function judge(tempValue) {
    var temp = '<select  style="width: 100px;height: 30px;" >';
    var flag = 0;
    $.each(category, function (i, item) {
        if (item.parent_id == tempValue) {
            temp += '<option value="' + item.id + '">' + item.name + '</option>';
            flag++;
        }
    });
    temp += '</select>';
    if (flag > 0) {
        mydiv.append(temp);
    }


}

